function [newSeqString newSeq] = addBlanksAndTargets(baseSeq, numTargets, blankBlockLen, postBlankRefreshLen, postTargetRefreshLen, SOA)

if nargin < 6
    SOA = 1100;
end

TRDuration = 3000;
min_number_of_trials_without_targets_in_the_beginning = 20;
always_have_one_target_within_this_many_trials_to_the_end = 30;


seqLength = length(baseSeq);
numSyncTrials = lcm(SOA, TRDuration) / SOA; % Number of trials required to sync scanner-experiment

%numTrials = (base sequence length) + (targets and refresh after targets) +
% + (2 blank-blocks and refresh after blanks) + 
% + (the last blank-block that doesn't require refresh)
if length(blankBlockLen) == 1
    numTrials = seqLength + numTargets*(postTargetRefreshLen+1) + 2*(blankBlockLen+postBlankRefreshLen) + blankBlockLen;
elseif length(blankBlockLen) == 3
    numTrials = seqLength + numTargets*(postTargetRefreshLen+1) + (blankBlockLen(1)+postBlankRefreshLen) + (blankBlockLen(2)+postBlankRefreshLen) + blankBlockLen(3);
end

if mod(numTrials,numSyncTrials) ~= 0
    error('Final sequence length does not allow proper syncing. Check number of targets and refresh-block lengths.');
end


%% Create a string to design the sequence

newSeqString = blanks(numTrials);
newSeqString(newSeqString==' ') = '-';
newSeqString = reshape(newSeqString,numSyncTrials,numTrials/numSyncTrials);


%% Add blank blocks

blankBlock = [];
for i=1:blankBlockLen(1)
    blankBlock = [blankBlock; 'B'];
end

postBlankRefreshBlock = [];
for i=1:postBlankRefreshLen
    postBlankRefreshBlock = [postBlankRefreshBlock; 'R'];
end

% Add first block of blanks
newSeqString(1:blankBlockLen) = blankBlock;
newSeqString((blankBlockLen+1):(blankBlockLen+postBlankRefreshLen)) = postBlankRefreshBlock;

% Add second block of blanks
if length(blankBlockLen) == 3
    blankBlock = [];
    for i=1:blankBlockLen(2)
        blankBlock = [blankBlock; 'B'];
    end
    if mod(blankBlockLen(2),2) == 0
        newSeqString(ceil(median(1:length(newSeqString(:))) - blankBlockLen(2)/2):floor(median(1:length(newSeqString(:))) + blankBlockLen(2)/2)) = blankBlock;
        newSeqString((floor(median(1:length(newSeqString(:))) + blankBlockLen(2)/2) + 1):(floor(median(1:length(newSeqString(:))) + blankBlockLen(2)/2)+postBlankRefreshLen)) = postBlankRefreshBlock;
    else
        newSeqString(ceil(median(1:length(newSeqString(:))) - blankBlockLen(2)/2):floor(median(1:length(newSeqString(:))) + blankBlockLen(2)/2)-1) = blankBlock;
        newSeqString((floor(median(1:length(newSeqString(:))) + blankBlockLen(2)/2)):(floor(median(1:length(newSeqString(:))) + blankBlockLen(2)/2)+postBlankRefreshLen-1)) = postBlankRefreshBlock;
    end
else
    if mod(blankBlockLen,2) == 0
        newSeqString(ceil(median(1:length(newSeqString(:))) - blankBlockLen/2):floor(median(1:length(newSeqString(:))) + blankBlockLen/2)) = blankBlock;
        newSeqString((floor(median(1:length(newSeqString(:))) + blankBlockLen/2) + 1):(floor(median(1:length(newSeqString(:))) + blankBlockLen/2)+postBlankRefreshLen)) = postBlankRefreshBlock;
    else
        newSeqString(ceil(median(1:length(newSeqString(:))) - blankBlockLen/2):floor(median(1:length(newSeqString(:))) + blankBlockLen/2)-1) = blankBlock;
        newSeqString((floor(median(1:length(newSeqString(:))) + blankBlockLen/2)):(floor(median(1:length(newSeqString(:))) + blankBlockLen/2)+postBlankRefreshLen-1)) = postBlankRefreshBlock;
    end
end

% Add second block of blanks
if length(blankBlockLen) == 3
    blankBlock = [];
    for i=1:blankBlockLen(2)
        blankBlock = [blankBlock; 'B'];
    end
    newSeqString(end-(blankBlockLen(3)-1):end) = blankBlock;
else
    newSeqString(end-(blankBlockLen-1):end) = blankBlock;
end



%% Add target trials

if numTargets > 0
    
    candidateTrials = find(newSeqString=='-');
    
    % Cut out the first few trials
    candidateTrials = candidateTrials(min_number_of_trials_without_targets_in_the_beginning:end);
    % Also, remove the very last trials to allow for the refresh block to be
    % included after the last target
    candidateTrials = candidateTrials(1:(end-(postTargetRefreshLen+postBlankRefreshLen)));
    % Finally, remove the few trials right before the middle blank block, to
    % allow for the refresh block to be included after a possible target
    candidateTrials = candidateTrials(~ismember(candidateTrials,(find(newSeqString(:)=='B') - (postTargetRefreshLen+postBlankRefreshLen))));
    
    % Now, pick numTargets that are spaced apart at least by 10 elements, and that is
    % not more than a few elements before the end (specified as a parameter)
    targetPositions = ones(1,numTargets);
    while (min(targetPositions(2:end)-targetPositions(1:end-1)) < 20) || (max(targetPositions) < (length(newSeqString(:))+1 - always_have_one_target_within_this_many_trials_to_the_end))
        % Pick numTargets arbitrary numbers from candidateTrials
        targetPositions = candidateTrials(sort(ceil(rand(1,numTargets)*length(candidateTrials))));
    end
    
    targetBlock = 'T';
    for i=1:postTargetRefreshLen
        targetBlock = [targetBlock 'R'];
    end
    
    for i=1:length(targetPositions)
        newSeqString(targetPositions(i):(targetPositions(i)+postTargetRefreshLen)) = targetBlock;
    end

end


%% Now, create the real sequence
newSeq = nan(size(newSeqString));

trialIndex = 1;

% Add valid trials
for i=1:length(newSeq(:))
    if newSeqString(i) == '-'
        newSeq(i) = baseSeq(trialIndex);
        trialIndex = trialIndex + 1;
    end
end

% Add overlap trials
copyingOverlapStarted = 0;
j = nan;
this_is_a_target_refresh_block = false;
this_is_a_blank_refresh_block = false;
for i=1:length(newSeq(:))
    if ((newSeqString(i) == 'R') && (copyingOverlapStarted == 0))
        copyingOverlapStarted = 1;
        j=i;
        % Determine what type of refresh block this is
        if newSeqString(i-1) == 'T'
            this_is_a_target_refresh_block = true;
        elseif newSeqString(i-1) == 'B'
            this_is_a_blank_refresh_block = true;
        end
        % Find the last valid trial and save as j
        while newSeqString(j) ~= '-'
            if j > 1
                j = j - 1;
            else
                j = length(newSeq(:));
            end 
        end
        if this_is_a_target_refresh_block
            j = j - (postTargetRefreshLen-1);
        elseif this_is_a_blank_refresh_block
            j = j - (postBlankRefreshLen-1);
        end
        newSeq(i) = newSeq(j);
        j = j + 1;
    elseif ((newSeqString(i) == 'R') && (copyingOverlapStarted == 1))
        newSeq(i) = newSeq(j);
        j = j + 1;
    else
        copyingOverlapStarted = 0;
        j = nan;
        this_is_a_target_refresh_block = false;
        this_is_a_blank_refresh_block = false;
    end
end

for i=1:length(newSeq(:))
    switch newSeqString(i)
        case 'B'
            newSeq(i) = 0;
        case 'T'
            newSeq(i) = -1;
    end
end

if sum(isnan(newSeq(:)))>0
    error('NaN elements detected in the final sequence. Check');
end


